package net.ib.mtalk.receiver;

import net.ib.mtalk.global.GlobalPreferences;
import net.ib.mtalk.network.NetState;
import net.ib.mtalk.service.SelecterService;
import net.ib.mtalk.util.MTalkLogWriter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;

/**
 * A BroadcastReciever Class that Receives the Net State Change Event.
 * @author	urunimi@infobank.net, Arngard
 */
public class NetStateReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		//MTalkLogWriter.v("NetStateReceiver.onReceive() - start");

		if (intent.getAction() == null) {
			//MTalkLogWriter.v("NetStateReceiver.onReceive() - end with no work");
			return;
		}

		if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
			int state = NetState.getNetState(context);

			if ( state >= 0 ) {
				MTalkLogWriter.d("NetState Changed: " + state + "(0:wifi, 1:mobile)");
				if (GlobalPreferences.getRegistrationId() == null) {
					SelecterService.actionStart(context);//LocalService.actionStart(context);
				} else {
					//MTalkLogWriter.d("NetStateReceiver.onReceive(): There's C2DM RegistrationId.");
				}
			} else {
				MTalkLogWriter.d("NetState Changed: Disconneted");
				SelecterService.actionStop(context);//LocalService.actionStop(context);
			}
		}

		//MTalkLogWriter.v("NetStateReceiver.onReceive() - end");
	}

}
